package com.drug.mingkang.controller;

import com.drug.mingkang.model.ImmSell;
import com.drug.mingkang.permission.model.PerUsers;
import com.drug.mingkang.service.ImmSellService;
import com.drug.mingkang.utils.ResponseMessage;
import com.drug.mingkang.utils.Tools;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

/**
 * (ImmSell)表控制层
 * 分店销售
 *
 * @author wutao
 * @since 2020-08-11 12:00:14
 */
@RestController
@RequestMapping("immSell")
public class ImmSellController {
    /**
     * 服务对象
     */
    @Resource
    private ImmSellService immSellService;

    @Resource
    private HttpSession session;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public ImmSell selectOne(String id) {
        return this.immSellService.queryById(id);
    }

    @ResponseBody
    @RequestMapping("show")
    public Map<String,Object> show(ImmSell immSell){
        PerUsers perUsers = (PerUsers) session.getAttribute("perUsers");
        String companyId = perUsers.getCompanyId();
        List<ImmSell> list = immSellService.queryAllByLimit(immSell,companyId);
        int count = immSellService.countId(immSell,companyId);
        Tools tools = new Tools();
        return tools.limit(list,count);
    }

    @ResponseBody
    @RequestMapping("shows")
    public Map<String,Object> shows(ImmSell immSell){
        PerUsers perUsers = (PerUsers) session.getAttribute("perUsers");
        String companyId = perUsers.getCompanyId();
        List<ImmSell> list = immSellService.queryAllByLimits(immSell,companyId,null);
        int count = immSellService.countIds(immSell,companyId);
        Tools tools = new Tools();
        return tools.limit(list,count);
    }

    @RequestMapping("deleteById")
    @ResponseBody
    public ResponseMessage deleteById(String sellId){
        int rows = immSellService.deleteById(sellId);
        if(rows > 0){
            return ResponseMessage.success("成功");
        }else {
            return ResponseMessage.error("失败");
        }
    }

    /**
     * 修改状态
     * @param sellId
     * @return
     */
    @PostMapping("updateStatus")
    public ResponseMessage updateStatus(String sellId){
        int rows = immSellService.updateStatus(sellId);
        return rows > 0 ? ResponseMessage.success("操作成功") : ResponseMessage.error("操作失败");
    }

}